import requests
import json
import execjs
import base64
import ddddocr
from Encrypt import get_collect, AES_Encrypt, get_data, AES_Decrypt

s = requests.session()
# 获得collect值
collect = get_collect()

# 获得图片，y，time_id
headers = {
    "authority": "captcha.misiai.com",
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
    "origin": "https://juyunso.com",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}
url = "https://captcha.misiai.com/v1/verify/generate"
params = {
    "app_id": "1000"
}
response = s.get(url, headers=headers, params=params)
# print(response.json())
data = response.json().get('data')
data = eval(AES_Decrypt(data))

bg_img = data.get('bg')
hk_img = data.get('img')
time_id = data.get('time_id')
y = data.get('y')
print(time_id)
print(y)
# 保存图片
bg_img = base64.urlsafe_b64decode(bg_img)
hk_img = base64.urlsafe_b64decode(hk_img)
with open('bg_img.png', "wb") as f:
    f.write(bg_img)
with open('hk_img.png', 'wb') as f:
    f.write(hk_img)

# 图片处理识别
from PIL import Image

img = Image.open("bg_img.png")
# print(img.size)
cropped = img.crop((0, 0, 310, y + 55))  # (left, upper, right, lower) 左上，右下
# cropped1 =img.crop((212, 34, 310, 160))
# Image._show(cropped)
cropped.save('bg_img_cut.png')
# Image._show(cropped1)

det = ddddocr.DdddOcr(det=False, ocr=False)
with open('hk_img.png', 'rb') as f:
    target_bytes = f.read()

with open('bg_img_cut.png', 'rb') as f:
    background_bytes = f.read()

res = det.slide_match(target_bytes, background_bytes)
x = res['target'][0:1][0] - 5
print(x)
# key_data = ctx.call('get_data', time_id, x, y)
key_data = get_data(time_id, x, y)
print(key_data)

# 验证是否通过
headers = {
    "authority": "captcha.misiai.com",
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
    "content-type": "application/json",
    "origin": "https://juyunso.com",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}
url = "https://captcha.misiai.com/v1/verify/slide"
data = {
    "data": key_data,
    "collect": collect
}
data = json.dumps(data)
response = s.post(url, headers=headers, data=data)
print(response.json())

if response.json().get('data') == True:
    # 过滑块
    headers = {
        "authority": "api.juyunso.com",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
        "content-type": "application/json",
        "origin": "https://juyunso.com",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
    }
    url = "https://api.juyunso.com/v1/verify/slide"
    data = {
        "time_id": time_id
    }
    data = json.dumps(data)
    response = s.post(url, headers=headers, data=data)
    print(response.json())
    recaptcha = response.json().get('data')

    # # 登陆
    # headers = {
    #     "authority": "api.juyunso.com",
    #     "accept": "application/json, text/plain, */*",
    #     "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
    #     "content-type": "application/json",
    #     "origin": "https://juyunso.com",
    #     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
    # }
    # url = "https://api.juyunso.com/v1/user/login"
    # data = {
    #     "username": "zh666",
    #     "password": "zjh6666",
    #     "recaptcha": recaptcha
    # }
    # data = json.dumps(data)
    # response = s.post(url, headers=headers, data=data)
    # token = response.json().get('data').get('token')
    # print(response.json())

    # 搜索
    headers = {
        "authority": "api.juyunso.com",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8",
        # "authorization": f"Bearer {token}",
        "origin": "https://jujuso.com",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
    }
    url = "https://api.juyunso.com/v1/api_search/free"
    params = {
        "page": "1",
        "size": "15",
        "kw": "啦啦啦",
        "kid": recaptcha,
        # "only_free": "1"
    }
    response = requests.get(url, headers=headers, params=params)
    print(response.text)
    print(response)
